<?php
//--------------------------------------------------------//
//名字:uuid()
//功能:根据当前时间（微秒计）生成唯一id(http://www.jb51.net/article/34636.htm)
//作者:欧祖新
//写于:2017-05
//整理:2017-05
//--------------------------------------------------------//
function uuid(){
	if(function_exists('com_create_guid')){
		return com_create_guid();
	}else{
		mt_srand((double)microtime()*10000);
		$charid=strtoupper(md5(uniqid(s,true)));//根据当前时间（微秒计）生成唯一id.
		//$hyphen=chr(45);//"-"
		//$uuid=substr($charid,0,8).$hyphen.substr($charid,8,4).$hyphen.substr($charid,12,4).$hyphen.substr($charid,16,4).$hyphen.substr($charid,20,12);//chr(125);"}"
		$uuid=$charid;//chr(125);"}"
		return $uuid;
	}
}
//--------------------------------------------------------//
//名字: CountByKey($stable,$sfield,$skey)
//功能:从$stable中,字段$sfield,关键字$skey,检查是否存在有数据
//作者:欧祖新
//写于:2017-03
//整理:2017-03
function CountByKey($stable,$sfield,$skey)
{
  global $db;
  $sql="select * from ".$stable . " where ". $sfield . "='". $skey . "'";
  $query=$db->query($sql);	
  $num=$db->num_rows($query);
  $db->free_result($query);
  If($num>=1)
  {
	return true;
  }   
  else
  {
	return false;
  }	   
}

function CountByKeys($stable,$sfield,$skey,$sfield1,$skey1)
{
  global $db;
  $sql="select * from ".$stable . " where ". $sfield . "='". $skey . "' and ".$sfield1."='".$skey1."'";  
  $query=$db->query($sql);	
  $num=$db->num_rows($query);
  $db->free_result($query);
  If($num>=1)
  {
	return true;
  }   
  else
  {
	return false;
  }	   
}
//--------------------------------------------------------//

//-------------------------------------------------------//
//名字:check_email($email)
//功能:封装邮箱验证函数。
//编写:2017-3-10 by ouxin
//
function check_email($email){
	if(ereg('^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+',$email)){
	return true;
	}else{
	return false;
	}
}
//-------------------------------------------------------//
//名字:phpbox($txt)
//功能:弹出一个提示框，$txt为要弹出的内容。
//编写:2016-7-7 by ouxin
function PhpBox($txt)
{
  echo "<script language='JavaScript'>alert('".$txt."')</script>";
}
//--------------------------------------------------------//

//-------------------------------------------------------//
//名字:GotoPage($page)
//功能:跳转到页面$page
//编写:2016-7-7 by ouxin
function GotoPage($page)
{
  if($page=='h')
    $page="javascript: history.back(1)";
  echo "<script language='JavaScript'>location='".$page."';</script>";
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字:listbody()
//功能:显示信息内容；
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function ListBody($body)
{
  //return nl2br(str_replace(" ","&nbsp; ",htmlspecialchars($body)));
  $tmpStr1 = htmlspecialchars($body);
  $tmpStr1 = ereg_replace(" ","&nbsp;",$tmpStr1);
  $tmpStr1 = ereg_replace(" ","&nbsp;&nbsp;",$tmpStr1);
  $tmpStr1 = ereg_replace("\r\n","<br>",$tmpStr1);
  $tmpStr1 = ereg_replace("\r","<br>",$tmpStr1);
  $tmpStr1 = ereg_replace("\n","<br>",$tmpStr1);
  return $tmpStr1;
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字:CheckId($ID)
//功能:检查正整数的合法性，返回BOOL
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function CheckId($Id)
{
  return eregi("^[0-9]*[0-9]*$",$Id) && strlen($Id)>0 ;
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字:isvalidemail($Email) 
//功能:检查电子邮件的合法性
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function IsvalidEmail($Email)
{
  if(!ereg("^[[:alnum:]\_\.]+@([[:alnum:]\_]+\.)+[[:alnum:]][[:alnum:]][[:alnum:]]?$",$Email, $regs))
  { 
    $isvalidemail = false; 
  } 
  else 
  { 
    $isvalidemail = true; 
  }	
  return $isvalidemail; 
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字: GetValueByKey($stable,$sfield,$skey,$sfieldvalue)
//功能:从$stable中,提取$sfieldvalue，字段$sfield,关键字$skey,条件是只有一条数据
//例如：根据ID获取名称 
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function GetValueByKey($stable,$sfield,$skey,$sfieldvalue)
{
  global $db;
  $sql="select * from ".$stable." where ".$sfield."='".$skey."'";  
  $query=$db->query($sql);
  $result=$db->fetch_array($query);  
  return str_replace("*","",$result[$sfieldvalue]);
  $db->free_result($result);
}

function GetValueByKeys($stable,$sfield,$skey,$sfield1,$skey1,$sfieldvalue)
{
  global $db;  
  $sql="select * from ".$stable . " where ". $sfield . "='". $skey . "' and ".$sfield1."='".$skey1."'";    
  $query=$db->query($sql);
  $result=$db->fetch_array($query);
  if($stable=="bas_type")
  {
     return "<a href='".$result["URL"]."?TYPE_ID=".$result["BAS_ID"]."'>".str_replace("*","",$result[$sfieldvalue])."</a>";
  }
  else
  {
     return str_replace("*","",$result[$sfieldvalue]);
  }  
  $db->free_result($result);
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字:GetIP()
//功能:获取客户端IP地址 
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function GetIP()
{
	$onlineip="";
	if(getenv('HTTP_CLIENT_IP')) $onlineip = getenv('HTTP_CLIENT_IP');
	elseif(getenv('HTTP_X_FORWARDED_FOR')) $onlineip = getenv('HTTP_X_FORWARDED_FOR');
	elseif(getenv('REMOTE_ADDR')) $onlineip = getenv('REMOTE_ADDR');
	else $onlineip = $_SERVER['REMOTE_ADDR']; 
	if($onlineip=="") $onlineip="Unknown";
	return $onlineip;
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字:GetOSInfo()
//功能:获取客户端OS
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function GetOSInfo()
{
	$os="";
	$Agent = $GLOBALS["HTTP_USER_AGENT"];
	if (eregi('win',$Agent) && strpos($Agent, '95')) $os="Win95";
	elseif (eregi('win 9x',$Agent) && strpos($Agent, '4.90')) $os="WinME";
	elseif (eregi('win',$Agent) && ereg('98',$Agent)) $os="Win98";
	elseif (eregi('win',$Agent) && eregi('nt 5\.0',$Agent)) $os="Win2000"; 
	elseif (eregi('win',$Agent) && eregi('nt 5\.1',$Agent)) $os="WinXP"; 
	elseif (eregi('win',$Agent) && eregi('nt',$Agent)) $os="WinNT";
	elseif (eregi('win',$Agent) && ereg('32',$Agent)) $os="Win32";
	elseif (eregi('linux',$Agent)) $os="Linux";
	elseif (eregi('unix',$Agent)) $os="Unix";
	elseif (eregi('sun',$Agent) && eregi('os',$Agent)) $os="SunOS";
	elseif (eregi('ibm',$Agent) && eregi('os',$Agent)) $os="IBM OS/2";
	elseif (eregi('Mac',$Agent) && eregi('PC',$Agent)) $os="Macintosh";
	elseif (eregi('PowerPC',$Agent)) $os="PowerPC";
	elseif (eregi('AIX',$Agent)) $os="AIX";
	elseif (eregi('HPUX',$Agent)) $os="HPUX";
	elseif (eregi('NetBSD',$Agent)) $os="NetBSD";
	elseif (eregi('BSD',$Agent)) $os="BSD";
	elseif (ereg('OSF1',$Agent)) $os="OSF1";
	elseif (ereg('IRIX',$Agent)) $os="IRIX";
	elseif (eregi('FreeBSD',$Agent)) $os="FreeBSD";
	 if ($os=='') $os = "Unknown";
	return $os;
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字:GetExplorerInfo()
//功能:获取客户端Explorer信息
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function GetExplorerInfo()
{
	$os=$_SERVER["HTTP_USER_AGENT"];
	if(strpos($os,"NetCaptor")) $explorer="NetCaptor";
	elseif(strpos($os,"MSIE 6")) $explorer="MSIE 6.x";
	elseif(strpos($os,"MSIE 5")) $explorer="MSIE 5.x";
	elseif(strpos($os,"MSIE 4")) $explorer="MSIE 4.x";
	elseif(strpos($os,"Netscape")) $explorer="Netscape";
	elseif(strpos($os,"Opera")) $explorer="Opera";
	else $explorer="Unknown";
	return $explorer;
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//格式化HTML字符串
//输入：HTML字符串
//输出：字符串
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function Html2Text($html)
{
	return htmlspecialchars(stripslashes($html));
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//格式化价格字符串
//输入：价格
//输出：带有逗号的价格字符串
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function MoneyFormat($price)
{
	return number_format($price, 2, '.', ',');
}
//--------------------------------------------------------//

//--------------------------------------------------------//
//名字:checkImgType($image_type,$image_name)
//功能:检查文件格式
//编写:2016-7-7 by ouxin
//--------------------------------------------------------//
function CheckFileType($file_type, $file_name)
{
  $temp=array('.php','.php3','.php4','.htm','.js','.xml','html');
  $sResourceType=strtolower(strrchr($file_name,"."));
  if (in_array( $sResourceType,$temp))  
  /*if((strcmp($file_type,"text/html")==0)||(strcmp($file_type,"text/plain")==0)||
  (strrchr($file_name,".")==".php3") || (strrchr($file_name,".")==".php") || (strrchr($file_name,".")==".php4") ||
  (strrchr($file_name,".")==".js") )
  */
  {
	$txt=$file_name."文件格式不对，不能上传！";
    $txt.="不能上传(.php)、(.php3)、(.php4)、(.htm)、(.html)、(.xml)、(.js)类型的文件。";
    phpbox($txt);
    GotoPage("h");
    return "err";
  }
  else
  {
  	$fileExt=strrchr($file_name,".");
    return $fileExt;
  }
}
//--------------------------------------------------------//

//-------------------------------------------------------//
//名字:T($time)
//功能:时间格式定义
//使用方法:echo T("时间戳")
//编写:2016-7-7 by ouxin
//-------------------------------------------------------//
function T($time)
{
   //获取今天凌晨的时间戳
   //$day = strtotime(date('Y-m-d',time()));
   //获取昨天凌晨的时间戳
   //$pday = strtotime(date('Y-m-d',strtotime('-1 day')));
   //获取现在的时间戳
   $nowtime = time();
   
   $tc = $nowtime-$time;
   if($tc>60*60*24*30*12){
      $str = floor($tc/(60*60*24*30*12))."年前";
   }elseif($tc>60*60*24*30){
      $str = floor($tc/(60*60*24*30))."月前";
   }elseif($tc>60*60*24*7){
      $str = floor($tc/(60*60*24*7))."周前";
   }elseif($tc>60*60*24){
      $str = floor($tc/(60*60*24))."天前";
   }elseif($tc>60*60){
      $str = floor($tc/(60*60))."小时前";
   }elseif($tc>60){
      $str = floor($tc/60)."分钟前";
   }else{
      $str = "刚刚";
   }
   return $str;
}
//-------------------------------------------------------//
?>